import { h, ref } from 'vue';
import { NSwitch } from 'naive-ui';
import { cloneDeep } from 'lodash-es';
import { FormSchema } from '@/components/Form';
import { defRangeShortcuts, formatToDate } from '@/utils/dateUtil';
import { renderOptionTag, renderPopoverMemberSumma, MemberSumma } from '@/utils';
import { Switch } from '@/api/auction';
import { useDictStore } from '@/store/modules/dict';
import { usePermission } from '@/hooks/web/usePermission';

const dict = useDictStore();
const { hasPermission } = usePermission();
const $message = window['$message'];

export class State {
  public id = 0; // id
  public name = ''; // 活动名称
  public startDate = ''; // 开始日期
  public endDate = ''; // 结束日期
  public status = 1; // 状态
  public enabled = 2; // 是否开启
  public createdAt = ''; // 创建时间
  public createdBy = 0; // 创建人
  public createdBySumma?: null | MemberSumma = null; // 创建人摘要信息
  public updatedAt = ''; // 更新时间

  constructor(state?: Partial<State>) {
    if (state) {
      Object.assign(this, state);
    }
  }
}

export function newState(state: State | Record<string, any> | null): State {
  if (state !== null) {
    if (state instanceof State) {
      return cloneDeep(state);
    }
    return new State(state);
  }
  return new State();
}

// 表单验证规则
export const rules = {
  name: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入活动名称',
  },
  startDate: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入开始日期',
  },
  endDate: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入结束日期',
  },
};

// 表格搜索表单
export const schemas = ref<FormSchema[]>([
  {
    field: 'name',
    component: 'NInput',
    label: '活动名称',
    componentProps: {
      placeholder: '请输入活动名称',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'status',
    component: 'NSelect',
    label: '状态',
    defaultValue: null,
    componentProps: {
      placeholder: '请选择状态',
      options: dict.getOption('auctionStatus'),
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'createdAt',
    component: 'NDatePicker',
    label: '创建时间',
    componentProps: {
      type: 'datetimerange',
      clearable: true,
      shortcuts: defRangeShortcuts(),
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
]);

// 表格列
export const columns = [
  {
    title: '活动名称',
    key: 'name',
    align: 'left',
    width: -1,
  },
  {
    title: '开始日期',
    key: 'startDate',
    align: 'left',
    width: -1,
    render(row: State) {
      return formatToDate(row.startDate);
    },
  },
  {
    title: '结束日期',
    key: 'endDate',
    align: 'left',
    width: -1,
    render(row: State) {
      return formatToDate(row.endDate);
    },
  },
  // {
  //   title: '状态',
  //   key: 'status',
  //   align: 'left',
  //   width: -1,
  //   render(row: State) {
  //     return renderOptionTag('auctionStatus', row.status);
  //   },
  // },
  {
    title: '是否开启',
    key: 'enabled',
    align: 'left',
    width: -1,
    render(row: State) {
      return h(NSwitch, {
        value: row.enabled === 1,
        checked: '开启',
        unchecked: '关闭',
        disabled: !hasPermission(['/auction/switch']),
        onUpdateValue: function (e) {
          console.log('onUpdateValue e:' + JSON.stringify(e));
          row.enabled = e ? 1 : 2;
          Switch({ id: row.id, key: 'enabled', value: row.enabled }).then((_res) => {
            $message.success('操作成功');
          });
        },
      });
    },
  },
  {
    title: '创建时间',
    key: 'createdAt',
    align: 'left',
    width: -1,
  },
  {
    title: '创建人',
    key: 'createdBy',
    align: 'left',
    width: -1,
    render(row: State) {
      return renderPopoverMemberSumma(row.createdBySumma);
    },
  },
  {
    title: '更新时间',
    key: 'updatedAt',
    align: 'left',
    width: -1,
  },
];

// 加载字典数据选项
export function loadOptions() {
  dict.loadOptions(['auctionStatus']);
}
